// pages/My/My.js
const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    currentTab: "我的博客",

    shopShow:false,
    shopShopBox:false,
    UserId:'',

    Collection:[],
    title:[],
    img:[],

    Bowman:[],
    showModal: false,
    autograph:'',
    autograph2:'',

    windowHeight:0
  },
  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  MyShop:function(){
    wx.navigateTo({
      url: "/pages/ShopAdmin/MyShop/MyShop",
    })
  },
  Order:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/Order/Order?order=" + e.currentTarget.dataset.id+ "",
    })
  },
  Bowman:function(e){
    wx.navigateTo({
      url: "/pages/BlogDetails/BlogDetails?id=" + e.currentTarget.dataset.id+ ""
    })
  },
  Collection:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/commodity/commodity?commodity=" + e.currentTarget.dataset.id+ "",
    })
  },
  ApplicationShop:function(){
    var that=this
    wx.showModal({
      title: '申请店铺',
      content: '是否申请店铺',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '申请中',
          })
          db.collection('User').doc(that.data.UserId).update({
            data: {
              // 表示指示数据库将字段自增 10
              type: '商家'
            },
            success: function(res) {
              console.log(res.data)
              that.setData({
                shopShow: true
              })
              wx.hideLoading();
              wx.showToast({
                title: "申请成功",
                icon:'none'
              })
            }
          })
        } else {
          console.log(res.confirm)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this 
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight)
          that.setData({
              windowHeight: res.windowHeight
          });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onAuth() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {//判断是否授权
          this.setData({
            isLogin: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("的加分加分")
    wx.cloud.callFunction({
      name: "login"
    }).then(res => {
      var openid=res.result.openid
      db.collection('User').where({
        _openid:openid
      }).get().then(res => {
        // res.data 包含该记录的数据
        console.log(res.data[0].type)
        if(res.data[0].type=='用户'){
          this.setData({
            shopShow: false,
            shopShopBox:true,
            UserId:res.data[0]._id,
            Collection:res.data[0].Collection,
            title:res.data[0].title,
            img:res.data[0].img,
            autograph:res.data[0].autograph
          })
        }else{
          this.setData({
            shopShow: true,
            shopShopBox:true,
            UserId:res.data[0]._id,
            Collection:res.data[0].Collection,
            title:res.data[0].title,
            img:res.data[0].img,
            autograph:res.data[0].autograph
          })
        }
      })
      db.collection('Bowman').where({
        _openid:openid
      }).get().then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
          this.setData({
            Bowman:res.data
          })
      })
      .catch(err => {
        console.log(err);
      })
    }).catch(err => {
      console.log(err);
    })
    
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {//判断是否授权
          this.setData({
            isLogin: true
          })
        } else {
          this.setData({
            isLogin: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
    * 弹窗
  */
  showDialogBtn: function () {
    this.setData({
      showModal: true,
      autograph2:this.data.autograph
    })
  },

  // 获取输入内容
  inputChange: function (e) {
    this.setData({
      autograph2: e.detail.value
    })
  },

  /**
    * 隐藏模态对话框
  */
  hideModal: function () {
    this.setData({
      showModal: false,
      autograph2: this.data.autograph,
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that=this
    if(that.data.ExpressNumber!=''){
      wx.showLoading({
        title: '修改中',
      })
      db.collection('User').doc(that.data.UserId).update({
        data: {
          // 表示指示数据库将字段自增 10
          autograph: that.data.autograph2
        }
      }).then(res => {
          console.log(res.data);
          that.setData({
            autograph:this.data.autograph2
          })
          wx.hideLoading();
          this.hideModal();
          wx.showToast({
            title: "修改成功",
            icon: "none"
          })
        }).catch(err => {
        console.log(err);
        wx.hideLoading();
        wx.showToast({
          title: "修改失败",
          icon: "none"
        })
      })
    }else{
      wx.showToast({
        title: "签名不能为空",
        icon: "none"
      })
    }
    
  },
})